package com.nantai.jiafang.nantaijiafang.deviceManager.preference;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.Place;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.ReasonBen;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.User;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by SY20140412 on 2015/5/5.
 */
public class PreferencesUtil {

    private static final String IS_Save = "is_save";
    private static final String UserList = "userList";
    private static final String PlaceList = "placeList";
    private static final String RepairReasonList = "repairReasonList";
    private static final String ScrapReasonList = "scrapReasonList";
    private static final String KeepReasonList = "keepReasonList";
    public final static String USER = "user";

    /**
     * 是否记住密码
    */
    public static boolean getIsSave(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        return publicPreferences.getBooleanValue(IS_Save, false);
    }

    /**
     *  是否记住密码
     */
    public static void setIsSave(Context context, boolean isLogin) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        publicPreferences.setValue(IS_Save, isLogin);
    }




    /**
     * 保存用户数据
     */
    public static void saveUser(Context context, User user) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        if (user != null) {
            publicPreferences.setValue(USER, new Gson().toJson(user));
        }
    }

    /**
     * 获取登录用户数据
     */
    public static User getUser(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        User user = null;
        if (publicPreferences.getStringValue(USER) != null) {
            user = new Gson().fromJson(publicPreferences.getStringValue(USER), User.class);
        }
        return user;
    }

    /**
     * 保存用户列表
     */
    public static void saveUserList(Context context, List<User> userlist) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        if (userlist != null) {
            publicPreferences.setValue(UserList, new Gson().toJson(userlist));
        }
    }

    /**
     * 获取用户列表
     */
    public static List<User> getUserList(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        List<User> user_list=new ArrayList<User>();
        Type type = new TypeToken<ArrayList<User>>() {}.getType();
        if (publicPreferences.getStringValue(UserList) != null) {
            user_list = new Gson().fromJson(publicPreferences.getStringValue(UserList), type);
        }
        return user_list;
    }


    /**
     * 保存库房列表
     */
    public static void savePlaceList(Context context, List<Place> placeList) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        if (placeList != null) {
            publicPreferences.setValue(PlaceList, new Gson().toJson(placeList));
        }
    }

    /**
     * 获取库房列表
     */
    public static List<Place> getPlaceList(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        List<Place> Place_list=new ArrayList<Place>();
        Type type = new TypeToken<ArrayList<Place>>() {}.getType();
        if (publicPreferences.getStringValue(PlaceList) != null) {
            Place_list = new Gson().fromJson(publicPreferences.getStringValue(PlaceList), type);
        }
        return Place_list;
    }

    /**
     * 保存维修原因列表
     */
    public static void saveRepairReasonList(Context context, List<ReasonBen>list ) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        if (list != null) {
            publicPreferences.setValue(RepairReasonList, new Gson().toJson(list));
        }
    }

    /**
     * 获取维修原因列表
     */
    public static List<ReasonBen> getRepairReasonList(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        List<ReasonBen> list=new ArrayList<ReasonBen>();
        Type type = new TypeToken<ArrayList<ReasonBen>>() {}.getType();
        if (publicPreferences.getStringValue(RepairReasonList) != null) {
            list = new Gson().fromJson(publicPreferences.getStringValue(RepairReasonList), type);
        }
        return list;
    }

    /**
     * 保存报废原因列表
     */
    public static void saveScrapReasonList(Context context, List<ReasonBen>list ) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        if (list != null) {
            publicPreferences.setValue(ScrapReasonList, new Gson().toJson(list));
        }
    }

    /**
     * 获取报废原因列表
     */
    public static List<ReasonBen> getScrapReasonList(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        List<ReasonBen> list=new ArrayList<ReasonBen>();
        Type type = new TypeToken<ArrayList<ReasonBen>>() {}.getType();
        if (publicPreferences.getStringValue(ScrapReasonList) != null) {
            list = new Gson().fromJson(publicPreferences.getStringValue(ScrapReasonList), type);
        }
        return list;
    }

    /**
     * 保存保养原因列表
     */
    public static void saveKeepReasonList(Context context, List<ReasonBen>list ) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        if (list != null) {
            publicPreferences.setValue(KeepReasonList, new Gson().toJson(list));
        }
    }

    /**
     * 获取保养原因列表
     */
    public static List<ReasonBen> getKeepReasonList(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        List<ReasonBen> list=new ArrayList<ReasonBen>();
        Type type = new TypeToken<ArrayList<ReasonBen>>() {}.getType();
        if (publicPreferences.getStringValue(KeepReasonList) != null) {
            list = new Gson().fromJson(publicPreferences.getStringValue(KeepReasonList), type);
        }
        return list;
    }


    /**
     * 删除User
     */
    public static void removeUser(Context context) {
        PublicPreferences publicPreferences = PublicPreferences.getInstance(context);
        publicPreferences.removeKey(USER);
    }


}

